package com.r2coding.music.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.r2coding.music.dao.SongCategoryDao;
import com.r2coding.music.dao.SongDao;
import com.r2coding.music.entity.SongCategoryEntity;
import com.r2coding.music.entity.SongEntity;
import com.r2coding.music.vo.req.QuerySingerListReqVo;
import com.r2coding.music.vo.resp.SongRespVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.r2coding.common.util.PageUtils;
import com.r2coding.common.util.Query;

import com.r2coding.music.dao.SingerDao;
import com.r2coding.music.entity.SingerEntity;
import com.r2coding.music.service.SingerService;
import org.springframework.util.CollectionUtils;

/**
 * 歌手
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-05-04 20:46:11
 */
@Service("singerService")
public class SingerServiceImpl extends ServiceImpl<SingerDao, SingerEntity> implements SingerService {

    @Autowired
    SongDao songDao;

    @Autowired
    SongCategoryDao songCategoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SingerEntity> page = this.page(
                new Query<SingerEntity>().getPage(params),
                new QueryWrapper<SingerEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageInfo<SingerEntity> selectList(QuerySingerListReqVo vo) {
        PageHelper.startPage(vo.getCurrent(), vo.getSize());
        QueryWrapper<SingerEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(vo.getSingerName())) {
            wrapper.like("singer_name", vo.getSingerName());
        }
        if (vo.getSingerSex() != null) {
            wrapper.eq("singer_sex", vo.getSingerSex());
        }
        wrapper.orderByDesc("create_time");
        List<SingerEntity> list = this.baseMapper.selectList(wrapper);
        return new PageInfo<SingerEntity>(list);
    }

    @Override
    public List<SongRespVo> getAllSongs(Integer id) {
        QueryWrapper<SongEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("singer_id", id);
        wrapper.orderByDesc("create_time");
        List<SongEntity> list = songDao.selectList(wrapper);
        List<SongRespVo> respVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            respVos = list.stream().map(item -> {
                SongRespVo respVo = new SongRespVo();
                // 根据歌手id查询歌手信息
                SingerEntity singer = this.baseMapper.selectById(id);
                BeanUtils.copyProperties(singer, respVo);
                BeanUtils.copyProperties(item, respVo);
                // 查询歌曲分类信息
                SongCategoryEntity songCategory = songCategoryDao.selectById(item.getSongCategoryId());
                BeanUtils.copyProperties(songCategory, respVo);
                respVo.setSongCategoryName(songCategory.getCategoryName());
                return respVo;
            }).collect(Collectors.toList());
        }
        return respVos;
    }
}